﻿<!doctype html>
<html>
<!-- InstanceBegin template="/Templates/template1.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<meta charset="utf-8">
<!-- InstanceBeginEditable name="doctitle" -->
<title>返利设置</title>
<!-- InstanceEndEditable -->
<script type="text/javascript" src="../js/common.js"></script>
<script type="text/javascript" src="../js/jquery.pager.js"></script>
<link href="../css/Pager.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../js/checkLogin.js"></script>
<script type="text/javascript" src="../js/utils.js"></script>
<script type="text/javascript" src="../js/formValidator.js"></script>
<script type="text/javascript" src="../js/formValidatorRegex.js"></script>
<link href="../css/validator3.css" type="text/css" rel="stylesheet" />
<link href="../css/style.css" type="text/css" rel="stylesheet" />
<!-- InstanceBeginEditable name="head" -->
<!-- InstanceEndEditable -->
<style type="text/css">
#memberList tr td:first {
display: none;
}
</style>
<script type="text/javascript">
var pagecount = 0;
var count = 0;
var pageIndex = 1 ;
$(function(){
	$.formValidator.initConfig({
		formID : "addR",
		onError : function() {
			alert("校验没有通过，具体错误请看错误提示");
		}
	});	
	
	$("#ruleName").formValidator({
		empty:false,
		onshow:"请输入字符",
		onfocus:"请输入字符(字母数字汉字下划线_及横线-)",
		oncorrect:"正确"
		 }).inputValidator({
			min:1,
			max:20,
			onerror:"不能为空(最大长度为20)"
		}).regexValidator({
			regexp:"^[A-Za-z0-9-_\u4e00-\u9fa5]{1,20}$",
			onerror:"请输入合法字符"});
	
	$("#rebaterMoney").formValidator({
		empty:false,
		onshow:"请输入金额",
		onfocus:"请输入金额",
		oncorrect:"正确"
		 }).inputValidator({
			min:1,
			max:20,
			onerror:"不能为空"
		}).regexValidator({
			regexp:"^[1-9][0-9]|0*(\.[0-9]{1,2})?$",
			onerror:"请输入合法金额"});
	$("#botNumber").formValidator({
		empty:false,
		onshow:"请输入正整数",
		onfocus:"请输入正整数",
		oncorrect:"正确"
		 }).inputValidator({
			min:1,
			max:20,
			onerror:"不能为空"
		}).regexValidator({
			regexp:"^[1-9]*[1-9][0-9]*$",
			onerror:"请输入合法字符"});
	
	
	
		
})
function  addRebateRules(){
	
	if($.formValidator.pageIsValid()){
	var  ruleName=$("#ruleName").val().replace(/\s/gi,'');
	var  rebaterMoney=$("#rebaterMoney").val().replace(/\s/gi,'');
	var botNumber=$("#botNumber").val().replace(/\s/gi,'');
	var rulesState=$("#rulesState").val()
	var heads = new Array("RuleName","money","botNumber","Status");
	var datas = new Array(ruleName,rebaterMoney,botNumber,rulesState);
	var parser = new Object();
	var jsonParserStr = null;
	if (IsNotEmpty(datas)){
		addToParser(heads, datas, "add" , parser);
		jsonParserStr =  JSON.stringify(parser);
	}
	
	$("#close_this").attr("disabled",true)
	$.post("../rebateRules/insert",{"data":jsonParserStr}, function(data) {
		$("#close_this").attr("disabled",false)
		var dt = JSON.parse(data);
		if(IsEmpty(dt)){
			art.dialog({fixed:true, lock: true,content:"数据添加错误",icon:'error',ok:true});	
			return false;
			}
			if(dt.state==0){
				pageIndex=1;
				InitTable();
				art.dialog({fixed:true, lock: true,content:"添加成功",icon:'succeed',ok:true});	
				$("#yinying1,#window_change").hide();
				 }else if(dt.state==1){
						art.dialog({fixed:true, lock: true,content:"添加出错",icon:'error',ok:true});	
			}else{
				checkStateShowErrorMsgAndDealResult(data);
			}	
		
	});
	
}

}




function  updatRebateRules(){
	if(!$.formValidator.pageIsValid()){
		return;
	}
	var  ruleName=$("#ruleName").val();
	var  rebaterMoney=$("#rebaterMoney").val();
	var botNumber=$("#botNumber").val();
	var rulesState=$("#rulesState").val()
	var updateId=$("#updateId").val();
	var heads = new Array("RuleName","money","botNumber","Status","id");
	var datas = new Array(ruleName,rebaterMoney,botNumber,rulesState,updateId);
	var parser = new Object();
	var jsonParserStr = null;
	if (IsNotEmpty(datas)){
		addToParser(heads, datas, "edit" , parser);
		jsonParserStr =  JSON.stringify(parser);
	}
	$("#close_this").attr("disabled",true)
	$.post("../rebateRules/edit",{"data":jsonParserStr}, function(data) {
		$("#close_this").attr("disabled",false)
		var dt = JSON.parse(data);
		if(IsEmpty(dt)){
			art.dialog({fixed:true, lock: true,content:"修改出错",icon:'error',ok:true});	
			return false;
			}
			if(dt.state==0){
				art.dialog({fixed:true, lock: true,content:"修改成功",icon:'succeed',ok:true});	
				InitTable()
				$("#yinying1,#window_change").hide();
				 }else if(dt.state==1){
						art.dialog({fixed:true, lock: true,content:"修改出错",icon:'error',ok:true});	
			}else{
				checkStateShowErrorMsgAndDealResult(data);
			}	
		
	});

}

function InitTable() {
	var name=$("#name").val().replace(/\s/gi,'');	
	var status=$("#status").val();
	
	var param={'rulesName':name,'status':status};
	
	$.ajax({
		type : "get",
		async:false,
		data :param,
		datatype : "json",
		url : "../rebateRules/find/rebateRulesList/" + pageIndex + "/10",
		success : function(data) {
			var rst = JSON.parse(data);	
					if(IsEmpty(rst)){
						art.dialog({fixed:true, lock: true,content:"查询出错",icon:'error',ok:true});	
						return false;
						}
						if(rst.state==0){
							$("#memberList tbody tr").remove();
							var obj = DealAndShowMsg(data,false,false);
							initTable(rst);					
							if(rst.result.len > 0){
								 var shu = rst.result.len;
							 	 pagecount = Math.ceil(shu/10);
							 	 $("#NoResult").hide();
							}else{
								$("#NoResult").show();
								pagecount=0;

							}
								
							 $("#sup").html("共"+rst.result.len+"条");	
							 $("#pager").pager({ pagenumber: pageIndex, pagecount: pagecount, buttonClickCallback: PageClick });
							 }else if(rst.state==1){
									art.dialog({fixed:true, lock: true,content:"查询出错",icon:'error',ok:true});	
						}else{
							checkStateShowErrorMsgAndDealResult(data);
						}		
					
		}
	
		
	});
	
	
	
		
	
}
PageClick = function(pageclickednumber) {
	pageIndex=pageclickednumber;
	InitTable();
}

function search(){
	InitTable();
	
}
function initTable(json){
	if(json!=null && json.state==0){
		var params = new Array("RuleName", "RebaterMoney","BotNumber","CreateFileTime", "StatusName");// "PayCash", "PayPos", "PayDepstAmt", "PayCheck", "PayPersonCard", "PayIntAmtMny", "StaName", "PaidName"
		Loadtable(json,params,"memberList");
	}
}

function Loadtable(json, params, tableId){
	var phArray = new Array();
	var count = 0;
	if (params != null) {
		$(params).each(function(pIndex, pData) {
			$.each(json.result.head, function(hIndex, hData) {
				if (pData == hData) {
					phArray[count++] = hIndex;
				}
			});
		});
	}
	if (json.result.data != null) {
		$.each(json.result.data, function(index, dt) {
			$("table[id='" + tableId + "']").append("<tr></tr>");
			$(phArray).each(function(phIndex, data) {
				$.each($(dt), function(dtIndex, d) {
					if (dtIndex == data) {
						$("table[id='" + tableId + "'] tr:last").append("<td>" +  (d === null||d=="null" ? " " : d) + "</td>");
					}
				});
			});
		});
		
	}
	
	
	for(i=0;i<json.result.data.length;i++){
		$("table[id='" + tableId + "'] tbody tr").eq(i).append("<td> <input type=\"hidden\" value=\""+json.result.data[i][3]+"\">  <input type=\"button\" class=\"common_btn to_update\" value=\"修改\" id=\""+json.result.data[i][0]+"\" ><br><input type=\"button\" class=\"del\" value=\"删除\" onclick=\"popPoint("+json.result.data[i][0]+")\">  </td>")
	}
	
	
	
}

function deletes(id){

	if(IsNotEmpty(id)){

		$.post("../rebateRules/del",{"id":id}, function(data) {
			var rst = JSON.parse(data);
			if(IsEmpty(rst)){
				art.dialog({fixed:true, lock: true,content:"删除出错",icon:'error',ok:true});	
				return false;
				}
				if(rst.state==0){
					art.dialog({fixed:true, lock: true,content:"删除成功",icon:'succeed',ok:true});	
					InitTable();
					if(pagecount==0&&pageIndex>0){
						pageIndex--
						InitTable();
					}
					}else if(rst.state==1){
						art.dialog({fixed:true, lock: true,content:"删除出错",icon:'error',ok:true});	
				}else{
					checkStateShowErrorMsgAndDealResult(data);
				}
			
		});
	}
}


</script>
</head>

<body>
<!-- InstanceBeginEditable name="EditRegion3" --> 
<script type="text/javascript">
$(function(){
	$("#closeThe").click(function(){
		$("#window_change,#yinying1").hide();
	})
	$("#addColumn").click(function(){
		
		$("#ruleName").val("");
		$("#rebaterMoney").val("");
		$("#botNumber").val("");
		$("#rulesState").val("")
		$("#updateId").val("");
		$("#yinying1,#window_change").show();
		$("#close_this").attr("onclick","addRebateRules()")

		$("#close_this").val("确认增加");	
		$("#close_this").attr("disabled",false);

	})
	$("#memberList tbody tr").each(function() {
		$(this).children("td:last").children("input").eq(1).click(function(){
			$(this).parents("tr").remove();
		})
     
    });
	addMargin($("#window_change"));
	$(".to_update").live("click",function(){
		var rulesName=$(this).parents("tr").children("td").eq(0).html();
		var money=$(this).parents("tr").children("td").eq(1).html();
		var cou=$(this).parents("tr").children("td").eq(2).html();
		var id=$(this).attr("id");
		var state=$(this).prev().val()
	$("#ruleName").val(rulesName);
	$("#rebaterMoney").val(money);
	$("#botNumber").val(cou);
	$("#updateId").val(id);
	$("#rulesState").val(state);
	$("#close_this").val("修改");
	$("#close_this").attr("onclick","updatRebateRules()")
	$("#yinying1,#window_change").show();
	addMargin($("#window_change"));
	
	
	
	
	
	})
		
})

function popPoint(id){
	art.dialog({fixed:true, lock: true,content:"确认删除该条数据？",icon:'question',ok: function(){ deletes(id)},cancel:true});
	}

</script>
<div id="yinying1"></div>
<form id="addR">
  <div id="window_change" class="member_search hideDiv">
    <div class="closeTop"><span id="closeThe"><img src="../images/close.png"></span></div>
    <p>
    <label>
    <span>名称：</span>
    <input type="text" id="ruleName" class="">
    <i id="ruleNameTip"></i>
    </label>
    </p>
    <p>
      <label><span>返现金额：</span>
        <input type="text" class="" id="rebaterMoney">
      </label>
      <i id="rebaterMoneyTip"></i></p>
    <p>
      <label><span>返现数量：</span>
        <input type="text" class="" id="botNumber">
      </label>
      <i id="botNumberTip"></i></p>
    <p><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;状态：</span>
      <select id="rulesState">
        <option value="0">使用</option>
        <option value="1">停用</option>
      </select>
      </label>
    </p>
    <div class="submit_btn">
      <input type="hidden" id="updateId" />
      <input type="button" value="保存信息" class="common_btn" id="close_this" onclick="addRebateRules()">
    </div>
  </div>
</form>
<div class="container" id="container">
  <div class="container_R fr">
    <div class="place">返利设置</div>
    <div class="member_search"> <span>名称：</span>
      <input type="text" id="name" class="absoluteTxt">
      <span>状态：</span>
      <select id="status">
        <option value="">所有</option>
        <option value="0">使用中</option>
        <option value="1">停用</option>
      </select>
      <input type="button" value="查询" class="btn" onclick="search()">
    </div>
    <table  width="100%" class="member_list" cellpadding="0" cellspacing="1" border="0" id="memberList">
      <caption>
      <strong>返利设置列表 <span id="sup">共0条</span></strong><input type="button" class="common_btn" value="增加" id="addColumn"> 
      </caption>
      <thead>
        <tr>
          <th>名称</th>
          <th>返现金额</th>
          <th>返利数量</th>
          <th>时间</th>
          <th>状态</th>
          <th>操作</th>
        </tr>
      </thead>
    </table>
     <div id="NoResult"  class="Nothing" style="text-align:center;display: none;">无符合条件的数据！</div>
    <div id="pager" style="float: right;"></div>
  </div>
  <div class="fc"></div>
</div>
<!-- InstanceEndEditable --> 
<script type="text/javascript" src="../js/header.js"></script> 
<script type="text/javascript" src="../js/left.js"></script> 
<script type="text/javascript" src="../js/footer.js"></script> 
<script type="text/javascript">
clearThis("absoluteTxt");
showTab(1,5);
</script>
</body>
<!-- InstanceEnd -->
</html>
